﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Web;
using Newtonsoft.Json;

namespace Korbit.Common
{
    public class Communication
    {
        public static string GetRequestData(string strURL, string strParameter)
        {
            string strReturn = "";
            HttpWebRequest request;

            try
            {
                request = (HttpWebRequest)WebRequest.Create(new Uri(strURL));
                request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
                request.ContentLength = strParameter.Length;

                //데이터 요청 타입 설정
                if (strParameter.Length == 0)
                {
                    request.Method = "GET";
                }
                else
                {
                    request.Method = "POST";

                    Stream stDataParams = request.GetRequestStream();
                    byte[] byteString = Encoding.UTF8.GetBytes(strParameter);
                    stDataParams.Write(byteString, 0, byteString.Length);
                    stDataParams.Close();
                }

                // 요청, 응답 받기
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                // 응답 Stream 읽기
                Stream stReadData = response.GetResponseStream();
                StreamReader srReadData = new StreamReader(stReadData, Encoding.UTF8);

                // 응답 Stream -> 응답 String 변환
                string strResult = srReadData.ReadToEnd();

                //디코딩
                strReturn = HttpUtility.UrlDecode(strResult);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            

            return strReturn;
        }

        public static string GetRequestData(string strURL)
        {
            string strReturn = "";

            strReturn = GetRequestData(strURL, "");

            return strReturn;
        }

        public static string GetRequestData(string strURL, Dictionary<string, string> dicParameter)
        {
            string strReturn = "";
            string strParameter = "";

            strParameter = CommonFunction.ConvertURLParameter(dicParameter);

            strReturn = GetRequestData(strURL, strParameter);

            return strReturn;
        }



    }
}
